/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fractionapplication;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 *
 * @author Ngocvan
 */
public class FractionApplication {

    private static ArrayList<Fraction> listFracs = new ArrayList<>();
    private static BufferedReader bfRr = new BufferedReader(new InputStreamReader(System.in));

    /**
     * @param args the command line arguments
     */
    public static int readFile() throws FileNotFoundException, IOException {
        String fileName = "data.txt";//default file name
//        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
//        System.out.print("Input file name");
//        fileName = bfRr.readLine();
        Scanner scanFile = new Scanner(new File(fileName));
        //process to read data from file

        while (scanFile.hasNext()) {
            Fraction frac = new Fraction(scanFile.nextInt(), scanFile.nextInt());
            listFracs.add(frac);
            System.out.print(frac);
        }
        System.out.println("Read file sucessfully !");
        System.out.println("*----------------------------*");
        return 0;
    }

    public static int inputtFraction() throws IOException {
        System.out.println("Please choose:");
        System.out.println("1.Input fraction from file");
        System.out.println("2.Input fraction manually");
        System.out.println("3.Return");
        int option = Integer.parseInt(bfRr.readLine());
        if (option == 1) {
            System.out.println("Read from file");
            readFile();
        } else {
            System.out.println("Please input fraction:");
            Fraction newFrac = new Fraction(Integer.parseInt(bfRr.readLine()), Integer.parseInt(bfRr.readLine()));
            listFracs.add(newFrac);
            System.out.println("Input sucessfully!");
        }
        return 0;
    }

    public static void printList() {
        System.out.print("List Fractions:");
        for (Fraction fraction : listFracs) {
            System.out.print(fraction);
        }
        System.out.println("");
        System.out.println("*----------------------------*");
    }

    public static void deleteElement() throws IOException {
        System.out.print("Input position you want to delete:");
        //        Fraction newFrac = new Fraction(Integer.parseInt(bfRr.readLine()), Integer.parseInt(bfRr.readLine()));
        int pos = Integer.parseInt(bfRr.readLine());
        //        listFracs.remove(newFrac);
        try {
            listFracs.remove(pos);
        } catch (Exception e) {
            System.out.println("Choose again, please!");
        }
        System.out.println("After delete:");
        printList();
        System.out.println("*----------------------------*");
    }

    private static void findMax() {
        Fraction maxFrac = listFracs.get(0);
        for (Fraction fraction : listFracs) {
            if (maxFrac.compareTo(fraction) < 0) {
                maxFrac = fraction;
            }
        }
        System.out.println("Max Faction:" + maxFrac);
        System.out.println("*----------------------------*");

    }

    public static void main(String[] args) throws InterruptedException, IOException {
        // TODO code application logic here
        int option = 1;

//        listFracs.add(new Fraction(1,2));
//        printList();
        while (option != 8) {
            try {
                //clear console if necessary
                printMenu();
//                String str = bfRr.readLine();
                option = Integer.parseInt(bfRr.readLine());
            } catch (IOException | NumberFormatException e) {
            }
            switch (option) {
                case 1:
                    inputtFraction();
                    break;
                case 2:
                    printList();
                    break;
                case 3:
                    deleteElement();
                    break;
                case 4:
                    System.out.println("Option is " + option);
                    break;
                case 5:
                    findMax();
                    break;
                case 6:
                    clearList();
                    break;
                case 7:
                    sortList();
                    break;
                case 8:
                    break;
                default:
                    System.out.println("Please reenter your choice !!!");
            }

        }
        //take options

        //Say goodbye
        System.out.println("Good bye !!!...");
        Thread.sleep(500);
    }

    private static void printMenu() {
        System.out.println("Fraction menu:");
        System.out.print("1. Import \t");
        System.out.print("2. Print \t");
        System.out.print("3. Delete element \t");
        System.out.print("4. Update \t");
        System.out.print("5. Find Max \t");
        System.out.print("6. Clear List \t");
        System.out.print("7. Sort \t");
        System.out.print("8. Exit \n");
        System.out.print("Please enter your choice:");
    }

    private static void clearList() {
        System.out.println("Clear list....");
        listFracs.clear();
        System.out.println("All has been gone !!!");
    }

    private static void sortList() {
        Collections.sort(listFracs);
        System.out.println("After sort:");
        printList();
    }
}
